﻿/*----------------------------------------------------------------
  // Copyright (C) 2011 江苏华招网
    // 版权所有。 
    // 作者：曹鹏
    //
    // 文件名：UserList.cs
    // 文件功能描述：主用户管理
    // 创建时间：2011年4月8日9:55:23
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 ------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using System.Data;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;

namespace HZ.Supervise.Web.Admin
{
    public partial class UserOperate : BasePage
    {

    
        IBLL_User iu = BLLFactory.BLL_User();
        IBLL_Role ir = BLLFactory.IBLL_Role();
        DataTable _roleOperateTable; 
        KeyOperationLogJG kj; 

        protected void Page_Load(object sender, EventArgs e)
        {
            string userId = Request.QueryString["uid"];

            if (!this.IsPostBack)
            {
                //获取主用户基本信息
                User user = iu.GetUserInfoByUserID(userId);

                if (user != null)
                {
                    lblUserId.Text = user.UserID;
                    lblUserName.Text = user.UserName;

                    if (userId != "root")
                    {

                        //用户所在角色的操作权限
                        IList<OperatingRule> roleOperateList = ir.GetRoleOperatingRuleByRoleID(user.UserRole.RoleID);
                        //IList<OperatingRule> roleOperateList = ir.GetRoleOperatingRuleByRoleID("03");
                        foreach (OperatingRule item in roleOperateList)
                        {
                            //if (!user.UserID.Contains("zbzx"))
                            //{
                                if (item.OperatingRuleName == "主用户管理")
                                {

                                    roleOperateList.Remove(item);

                                    break;

                                }
                            //}

                        }

                        //用户自己的操作权限
                        IList<OperatingRule> userOperateList = iu.GetUserOperatingRule(user.UserID);

                        foreach (OperatingRule item in userOperateList)
                        {
                            //if (!user.UserID.Contains("zbzx"))
                            //{
                                if (item.OperatingRuleName == "主用户管理")
                                {

                                    userOperateList.Remove(item);

                                    break;

                                }
                            //}

                        }

                        ////转换成DataTable
                        _roleOperateTable = HZ.Data.ModelConvert.GetDataTableFromModel(roleOperateList);
                        DataTable userOperateTable = HZ.Data.ModelConvert.GetDataTableFromModel(userOperateList);
                        userOperateTable.PrimaryKey = new DataColumn[] { userOperateTable.Columns["OperatingRuleID"] };

                        //添加自定义列 合并二张table
                        _roleOperateTable.Columns.Add(new DataColumn("IsUsers", typeof(System.Boolean)));
                        foreach (DataRow row in _roleOperateTable.Rows)
                        {
                            row["IsUsers"] = userOperateTable.Rows.Contains(row["OperatingRuleID"]);
                        }

                        //分组以获得模块列表
                        DataTable tableModule = _roleOperateTable.DefaultView.ToTable(true, "Module");
                        //添加模块枚举值列
                        tableModule.Columns.Add(new DataColumn("ModuleValue", typeof(System.Int32)));
                        foreach (DataRow row in tableModule.Rows)
                        {
                            row["ModuleValue"] = EnumParse.GetModule((int)row["Module"]);
                        }

                        //按模块枚举值排序
                        DataView dv = new DataView(tableModule);
                        dv.Sort = "ModuleValue asc";


                        this.rptModule.DataSource = dv;
                        this.rptModule.DataBind(); 
                    }
                }
                else
                {
                    PageError("用户不存在", "UserList.aspx");
                }
            }
        }

        /// <summary>
        /// 绑定rptOperate
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptModule_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataTable table;

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                table = this._roleOperateTable;
                if (table.Rows.Count > 0)
                {
                    DataView dv = new DataView(table);
                    dv.RowFilter = "Module ='" + DataBinder.Eval(e.Item.DataItem, "Module").ToString() + "'";
                    //dv.Sort = " OperatingRuleName";
                    Repeater rptStoreSubject = (Repeater)e.Item.FindControl("rptOperate");
                    rptStoreSubject.DataSource = dv;
                    rptStoreSubject.DataBind();
                }
            }
        }

        /// <summary>
        /// 保存设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string userId = Request.QueryString["uid"].ToString();
            User user = new User();
            user.UserID = userId;

            //获取用户的操作权限
            List<OperatingRule> operatingRuleList = new List<OperatingRule>();
            string ids = Request["chkOperate"];
            if (!string.IsNullOrEmpty(ids))
            {
                string[] arraryIds = ids.Split(',');
                foreach (string Item in arraryIds)
                {
                    OperatingRule otr = new OperatingRule();
                    otr.OperatingRuleID =Item;
                    operatingRuleList.Add(otr);
                }
            }

            //更新用户及用户权限
            if (iu.SetUserOperatingRule(userId, operatingRuleList))
            {
                AddLog(string.Format("设置用户[{0}]权限成功!",userId));
                if (iu.GetUserByUserID(userId).IsUserAdmin)
                {
                    PageInfo("用户权限更新成功！", this.ReturnUrl);
                }
                else
                {
                    PageInfo("用户权限更新成功！", this.ReturnUrl);
                } 
            }
            else
            {
                PageError("用户权限更新失败！", "");
            }


        }
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="msg"></param>
        private void AddLog(string msg)
        {
            kj = new KeyOperationLogJG();
            kj.OperationLogSerialID = Guid.NewGuid().ToString();
            kj.Operator = CurrentAccount.GetUser().UserID;
            kj.OrgID = CurrentAccount.GetUser().Org.OrgID;
            kj.OperateTime = DateTime.Now;
            kj.OperateMessage = msg;
            iu.AddKeyOperationLog(kj);
        }
        /// <summary>
        /// 转换布尔值为checked
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected string GetChecked(object obj)
        {
            if (obj.ToString() == true.ToString())
            {
                return "checked";
            }
            return "";
        }
    }
    
}
